package com.elephant.core.nio;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author: hai
 * @Date: 2018/12/29 10:21
 */
public class ReentrantLockDemo implements Runnable {

    private ReentrantLock lock = new ReentrantLock();
    private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    @Override
    public void run() {

        try {
            lock.lock();
            lock.lockInterruptibly();
            lock.tryLock();
            lock.lock();
            readWriteLock.readLock();
            readWriteLock.writeLock();
            Thread.sleep(1000);
            System.out.println(System.currentTimeMillis() + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            lock.unlock();
        }

    }

    public static void main(String[] args) {

        ReentrantLockDemo demo = new ReentrantLockDemo();
        Thread t1 = new Thread(demo, "thread1");
        Thread t2 = new Thread(demo, "thread2");

        t1.start();
        t2.start();

        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}
